package io.renren.modules.sys.common;

import io.renren.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;

/**
 * @Author: weihongyang
 * @Date: 2019/4/24 9:48
 * @Version 1.0
 */
@Service(value = "commonDataService")
public class CommonDataServiceImpl implements CommonDataService{
    public static String ACCESS_TOKEN = "AccessToken:{0}";

    @Autowired
    private RedisUtils redisUtil;

    @Override
    public void putCurrentUserDataToRedis(CurrentUser currentUser) {
        if (null == currentUser || StringUtils.isEmpty(currentUser.getToken())) {
            return;
        }

        String token = currentUser.getToken();
        String key = MessageFormat.format(ACCESS_TOKEN, token);
        redisUtil.set(key, currentUser);

//        redisUtil.expire(key, 60L * 60 * 8);
        //  redisUtil.expire(key, 10L);
    }

    @Override
    public void removeCurrentUserDataFromRedis(String token) {
        String key = MessageFormat.format(ACCESS_TOKEN, token);
        redisUtil.delete(key);
    }

    @Override
    public CurrentUser getCurrentUserDataFromRedis(String token) {
        String key = MessageFormat.format(ACCESS_TOKEN, token);
        return (CurrentUser)redisUtil.get(key);
    }
}
